// pages/goods-detail/goods-detail.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperIndex: 1, //轮播图当前位置
    slideUrls: [],
    buy_num: 1,
    is_show: false,
    lunbo_number: '',
    guigeInfo: [],
    taocan_id: '',
    taocan_detail: {},
    guige_id: '',
    index: 0,
    isSelect: 0,
  },
  swiperChange: function (e) {
    var that = this;
    // if (e.detail.current != 0) {
    //   that.data.videoContextCurrent.stop();
    // } else {
    //   that.data.videoContextCurrent.play();
    // }

    var current = e.detail.current
    that.setData({
      swiperIndex: current + 1
    })
  },
  go_shouye() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  to_goumai() {
    this.setData({
      is_show: true
    })
    // this.chooseGuige();

  },
  close() {
    this.setData({
      is_show: false
    })
  },

  //商品详情
  get_godsInfo() {
    let that = this;
    let taocan_id = that.data.taocan_id;
    common.request(api.get_Product_detail, {
      taocan_id: taocan_id
    }, 'post').then((res) => {
      
      if(res.taocan_detail.taocan_desc){
        res.taocan_detail.taocan_desc = res.taocan_detail.taocan_desc.replace(/<img/gi, '<img style="max-width:100%;height:auto;float:left;display:block" ');

      }

      console.log("商品详情====", res)
      that.setData({
        taocan_detail: res.taocan_detail,
        guigeInfo: res.taocan_detail.guige,
        slideUrls: res.lunbopic,
        lunbo_number: res.lunbopic.length,
        // guige_price:guige_price,
        // guige_name:guige_name
      })
      var guigeList = that.data.guigeInfo;
      for (let i = 0; i < guigeList.length; i++) {

        if (i == 0) {
          that.setData({
            guige_id: guigeList[0].id,

            guige_name: guigeList[0].guige_name,
            guige_price: guigeList[0].guige_price,
            guige_num: guigeList[0].guige_num,
          })
        }
      }
    })
  },
  //选择规格
  chooseGuige(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var name = e.currentTarget.dataset.name;
    var num = e.currentTarget.dataset.num;
    var price = e.currentTarget.dataset.price;
    var id = e.currentTarget.dataset.id;
    that.setData({
      isSelect: index,
      guige_id: id,
      guige_price: price,
      guige_name: name,
      guige_num: num,
    })


  
  },

  //确认订单页面
  to_confirmOrder(e) {
    let that = this;
    console.log(e)
    let taocan_id = that.data.taocan_detail.taocan_id;
    // let guige_id = that.data.taocan_detail.guige[0].id
    var user_id=wx.getStorageSync('user_id')
    if(user_id){
      wx.navigateTo({
        url: '/pages/order-confirm/order-confirm?taocan_id=' + taocan_id + '&guige_id=' + that.data.guige_id,
      })

    }else{
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
    }
   
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id)
    this.setData({
      taocan_id: options.id
    })
    this.get_godsInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})